package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 会员SERVICE实现类
 */
@Service(interfaceClass = MemberService.class)
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Transactional
    @Override
    public Member addIfNotExist(Member member) {
        log.info("[会员-添加]data:{}",member);
        Member pojo = memberDao.selectPhoneNumber(member.getPhoneNumber());
        if(null != pojo) {
            //会员已添加，直接返回
            return pojo;
        }
        //调用DAO插入数据
        memberDao.insert(member);
        return memberDao.selectById(member.getId());
    }

    @Override
    public Member findById(Integer id) {

        return memberDao.selectById(id);
    }



    @Override
    public List<Integer> countByMonth(List<String> months) {
        List<Integer> resultLIst = new ArrayList<>();
        for (String month : months) {
            String endDate = month+".31";
            Long count = memberDao.countByRegTimeBefore(endDate);
            resultLIst.add(count.intValue());
        }
        return resultLIst;
    }


    @Override
    public List<Map> getMemberCountGroupBySex() {
        //为性别为NULL的附加默认值
        memberDao.sexDefault();
        List<Map> memberCountGroupBySex= memberDao.getMemberCountGroupBySex();
        log.info("getMemberCountGroupBySex:小明"+memberCountGroupBySex.toString());
        for (Map map : memberCountGroupBySex) {
            if (map.get("name").equals("1")){
                map.put("name","男性");
            }else {
                map.put("name","女性");
            }
        }
        return memberCountGroupBySex;
    }

    @Override
    public List<Map> getMemberCountGroupByAge() {
        //为年龄为NULL的附加默认值
        memberDao.ageDefault();
        Map<String, Object> map1 = new HashMap<String, Object>();
        Map<String, Object> map2 = new HashMap<String, Object>();
        Map<String, Object> map3 = new HashMap<String, Object>();
        Map<String, Object> map4 = new HashMap<String, Object>();
        map1.put("name","0-18岁");
        map2.put("name","18-30岁");
        map3.put("name","30-45岁");
        map4.put("name",">45岁");

        List<String> list = memberDao.getMemberCountGroupByAge();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int aa = 0;
        int bb = 0;
        int cc = 0;
        int dd = 0;
        for (String s : list) {
            try {
                Date date = sdf.parse(s);
                int currentAge = DateUtils.getCurrentAge(date);
                if (currentAge>=0&&currentAge<=18){
                    aa+=1;
                }else if (currentAge>18&&currentAge<=30){
                    bb+=1;
                }else if (currentAge>30&&currentAge<=45){
                    cc+=1;
                }else if (currentAge>45){
                    dd+=1;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        map1.put("value",aa);
        map2.put("value",bb);
        map3.put("value",cc);
        map4.put("value",dd);

        List<Map> mapList= new ArrayList<>();
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);
        mapList.add(map4);
        return mapList;

    }
}
